using System;
using System.Collections.Generic;
using BetFairGlobal;


namespace BetFairProject
{
	public class Event : IEvent
	{
		private static BFGlobalService global = new BFGlobalService();
		
		public bool IsMarket 
		{
			get
			{
				return false;
			}
			set
			{
			}
			
		}
		
		public int EventType {get;set;}
		
		public bool IsLoaded {get;set;}
		
		public List<IEvent> EventChilds {get;set;}
		
		public string Name {get;set;}
		
		public int ID {get;set;}
		
		public IEvent Parent {get;set;}
		
		public Event ()
		{
			
		}
		
		public Event (BFEvent APIEvent)
		{
			IsLoaded = false;
			Name = APIEvent.eventName;
			ID = APIEvent.eventId;
		}
		
		public Event(APIRequestHeader header, int eventType)
		{
			
			GetEventsReq request = new GetEventsReq();
			
			request.header = header;
			
			request.eventParentId = eventType;
			
			request.locale = "pt";
			
			GetEventsResp resp = global.getEvents(request);
			
			Parent = null;
			IsLoaded = true;
			EventChilds = new List<IEvent>();
			foreach(var ev in resp.eventItems)
			{
				Event temp = new Event(ev);
				
				temp.Load(header);
				
				EventChilds.Add(temp);
			}
			
		}
		
		
		public void Load(APIRequestHeader header)
		{
			GetEventsReq request = new GetEventsReq();
			
			request.header = header;
			
			request.eventParentId = ID;
			
			request.locale = "pt";
			
			GetEventsResp resp = global.getEvents(request);
			
			Parent = null;
			IsLoaded = true;
			EventChilds = new List<IEvent>();
			foreach(var ev in resp.eventItems)
			{
				Event temp = new Event(ev);
				
				temp.Load(header);
				
				EventChilds.Add(temp);
			}
		}
		
		public override string ToString ()
		{
			string x = string.Format ("[Event: EventType={0}]", EventType);
			
			foreach(var ev in EventChilds)
			{
				if(ev.IsActive())
				{
					x += ev.Name + "\n";
				}
			}
			
			return x;
		}
		
		public bool IsActive()
		{
			return EventChilds.Count > 0;
		}
	}
}

